﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class main2 : System.Web.UI.Page
{
    CommonClass ccobj = new CommonClass();
    GoodsClass gcobj = new GoodsClass();
    DbClass dbobj = new DbClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            dlBind();
            deplayTitle();

        }
    }
    protected void dLRefine_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "buy")
        {
            //addshodcart(e);
            AddShopCart(e);
        }

    }
    public void deplayTitle()
    {
        if (this.Request.Params["var"] == "1")
        {
            //如果Request["var"]的值为1，表示点击头控件中的“新品上市”、“特价商品”和“热销商品”导航到该浏览页
            switch (this.Request.Params["id"])
            {
                case "1":
                    this.labpage.Text = "首页/新品上市";
                    break;
                case "2":
                    this.labpage.Text = "首页/精品推荐";
                    break;
                case "3":
                    this.labpage.Text = "首页/特价商品";
                    break;
                case "4":
                    this.labpage.Text = "首页/热销商品";
                    break;
            }

        }
        else
        {
            //表示点击分类导航条中的商品类别名导航到该浏览页
            string strClassName = gcobj.GetClass(Convert.ToInt32(this.Request.Params["id"].ToString()));
            this.labpage.Text = "首页/商品分类/" + strClassName;
        }
    }
    protected void dlBind()
    {
        if (this.Request.Params["var"] == "1")
        {
            dlBindPage(Convert.ToInt32(Request.Params["id"]), 0);
        }
        else
        {
            dlBindPage(0, Convert.ToInt32(Request.Params["id"]));
        }
    }
    protected void dlBindPage(int IntDeplay, int IntClass)
    {
        SqlCommand myCmd = dbobj.GetCommandProc("proc_GIList");
        SqlParameter Deplay = new SqlParameter("@Deplay", SqlDbType.Int, 4);
        Deplay.Value = IntDeplay;
        myCmd.Parameters.Add(Deplay);
        SqlParameter Class = new SqlParameter("@ClassID", SqlDbType.Int, 4);
        Class.Value = IntClass;
        myCmd.Parameters.Add(Class);
        dbobj.ExecNonQuery(myCmd);
        DataTable dsTable = dbobj.GetDataSet(myCmd, "tbGI");
        int curpage = Convert.ToInt32(this.dqy.Text);
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dsTable.DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 3;
        ps.CurrentPageIndex = curpage - 1;
        this.last.Enabled = true;
        this.final.Enabled = true;
        this.next.Enabled = true;
        this.final.Enabled = true;
        if (curpage == 1)
        {
            this.first.Enabled = false;
            this.last.Enabled = false;

        }
        if (curpage == ps.PageCount)
        {
            this.final.Enabled = false;
            this.next.Enabled = false;
        }
        this.zym.Text = Convert.ToString(ps.PageCount);
        this.dLRefine.DataSource = ps;
        this.dLRefine.DataKeyField = "BookID";
        this.dLRefine.DataBind();
    }
    /*public void deplayTitle()
    {
        if (this.Request["var"] == "1")
        {
 
        }
    }*/
    protected void first_Click(object sender, EventArgs e)
    {
        this.dqy.Text = "1";
        this.dlBind();
    }
    protected void last_Click(object sender, EventArgs e)
    {
        this.dqy.Text = Convert.ToString(Convert.ToInt32(this.dqy.Text) - 1);
        this.dlBind();
    }
    protected void next_Click(object sender, EventArgs e)
    {
        this.dqy.Text = Convert.ToString(Convert.ToInt32(this.dqy.Text) + 1);
        this.dlBind();
    }
    protected void final_Click(object sender, EventArgs e)
    {
        this.dqy.Text = this.zym.Text;
        this.dlBind();
    }
    public void AddShopCart(DataListCommandEventArgs e)
    {
        Hashtable hashCar;
        if (Session["ShopCart"] == null)
        {
            hashCar = new Hashtable();
            hashCar.Add(e.CommandArgument, 1);
            Session["ShopCart"] = hashCar;
        }
        else
        {
            hashCar = (Hashtable)Session["ShopCart"];
            if (hashCar.Contains(e.CommandArgument))
            {
                int count = Convert.ToInt32(hashCar[e.CommandArgument].ToString());
                hashCar[e.CommandArgument] = (count + 1);
            }
            else
            {
                hashCar.Add(e.CommandArgument, 1);
            }
        }
    }

}